package com.henry.db.migrate.task;

import com.henry.db.migrate.factory.DbFactory;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;

public class MigrateTask {

    private BaseTable baseTable;
    private int rowNum;
    private boolean exist;

    public MigrateTask(BaseTable baseTable) {
        this.baseTable = baseTable;
        Connection connection = DbFactory.getConnection("src");
        try {
            QueryRunner runner = new QueryRunner();
            Long count = runner.query(connection, baseTable.getSrcCountSql(), new ScalarHandler<>());
            this.rowNum=count.intValue();
            this.exist=true;
        } catch (Exception e) {
            e.printStackTrace();
            this.exist=false;
        }finally {
            DbFactory.close(connection);
        }
    }

    public BaseTable getBaseTable() {
        return baseTable;
    }

    public int getPageNum() {
        int pageSize=baseTable.pageSize;
        if(rowNum%pageSize==0)
            return rowNum/pageSize;
        return rowNum/pageSize+1;
    }
}
